<template>
  <view class="demo-container">
    <!-- 基础用法 -->
    <view class="demo-block">
      <view class="demo-block__title">基础用法</view>
      <view class="demo-block__content">
        <fu-field
          v-model:value="value1"
          label="日期时间"
          readonly
          placeholder="点击选择"
          @click="show1 = true"
        />
        <fu-datetime-picker
          v-model:show="show1"
          v-model:value="value1"
          @confirm="onConfirm1"
          @cancel="onCancel"
        />
      </view>
    </view>
    
    <!-- 选择日期 -->
    <view class="demo-block">
      <view class="demo-block__title">选择日期</view>
      <view class="demo-block__content">
        <fu-field
          v-model:value="value2"
          label="日期"
          readonly
          placeholder="点击选择"
          @click="show2 = true"
        />
        <fu-datetime-picker
          v-model:show="show2"
          v-model:value="value2"
          type="date"
          @confirm="onConfirm2"
          @cancel="onCancel"
        />
      </view>
    </view>
    
    <!-- 选择时间 -->
    <view class="demo-block">
      <view class="demo-block__title">选择时间</view>
      <view class="demo-block__content">
        <fu-field
          v-model:value="value3"
          label="时间"
          readonly
          placeholder="点击选择"
          @click="show3 = true"
        />
        <fu-datetime-picker
          v-model:show="show3"
          v-model:value="value3"
          type="time"
          @confirm="onConfirm3"
          @cancel="onCancel"
        />
      </view>
    </view>
  </view>
</template>

<script>
export default {
  data() {
    return {
      show1: false,
      show2: false,
      show3: false,
      value1: '',
      value2: '',
      value3: ''
    }
  },
  methods: {
    onConfirm1(value) {
      this.value1 = this.formatDate(value, 'yyyy-MM-dd HH:mm')
    },
    onConfirm2(value) {
      this.value2 = this.formatDate(value, 'yyyy-MM-dd')
    },
    onConfirm3(value) {
      this.value3 = this.formatDate(value, 'HH:mm')
    },
    onCancel() {
      uni.showToast({
        title: '取消选择',
        icon: 'none'
      })
    },
    formatDate(timestamp, format) {
      const date = new Date(timestamp)
      const year = date.getFullYear()
      const month = date.getMonth() + 1
      const day = date.getDate()
      const hour = date.getHours()
      const minute = date.getMinutes()
      
      format = format.replace(/yyyy/g, year)
      format = format.replace(/MM/g, month.toString().padStart(2, '0'))
      format = format.replace(/dd/g, day.toString().padStart(2, '0'))
      format = format.replace(/HH/g, hour.toString().padStart(2, '0'))
      format = format.replace(/mm/g, minute.toString().padStart(2, '0'))
      
      return format
    }
  }
}
</script>

<style lang="scss" scoped>
.demo-container {
  padding: 20rpx;
}

.demo-block {
  margin-bottom: 30rpx;
  
  &__title {
    margin-bottom: 20rpx;
    font-size: var(--fu-font-size-lg);
    color: var(--fu-text-color);
  }
  
  &__content {
    background-color: #fff;
    border-radius: var(--fu-radius-base);
    overflow: hidden;
  }
}
</style> 